{extend name='public/base' /}

{block name="content"}



<div class="layui-col-md12">
    <div class="layui-card">
        <div class="layui-card-body">
            <form action="" class="layui-form" method="get">
                <div class="layui-form-item layui-form-pane">
                    <div class="layui-inline">
                        <label class="layui-form-label">商户：</label>
                        <div class="layui-input-inline">
                            <select name="merchant_id" lay-verify="required" lay-search="">
                                <option value="0" ></option>
                                {volist name="merchant_list" id="t"}
                                {if empty($t.name)||$t.name=='-'}
                                <option value="{$t.id}" >{$t.company} - {$t.code}-{$t.biz_state}-{$t.positions_code}</option>
                                {else/}
                                <option value="{$t.id}" >{$t.name} - {$t.code}-{$t.biz_state}-{$t.positions_code}</option>
                                {/if}
                                {/volist}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">设备名称：</label>
                        <div class="layui-input-inline">
                            <input name="pointName" class="layui-input" id="pointName" value="" type="text"
                                   placeholder="请输入设备名称">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">设备编号：</label>
                        <div class="layui-input-inline">
                            <input name="pointId" class="layui-input" id="pointId" value="" type="text"
                                   placeholder="请输入设备编号">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">设备类型：</label>
                        <div class="layui-input-inline">
                            <select name="type" >
                                <option value="" >请选择</option>
                                <option value="1" >智能电表</option>
                                <option value="2" >智能水表</option>
                                <option value="3" >普通电表</option>
                                <option value="4" >普通水表</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">使用类型：</label>
                        <div class="layui-input-inline">
                            <select name="use_type" >
                                <option value="" >请选择</option>
                                <option value="1" >写字楼自用</option>
                                <option value="2" >写字楼公区</option>
                                <option value="3" >写字楼商户</option>
                                <option value="4" >商场自用</option>
                                <option value="5" >商场公区</option>
                                <option value="6" >商场商户</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" >特殊用表</label>
                        <div class="layui-input-inline" >
                            <select name="is_sole"  lay-filter="is_sole" id="is_sole">
                                <option value="">请选择</option>
                                <option value="0" >否</option>
                                <option value="1" >是</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" >是否总表</label>
                        <div class="layui-input-inline" >
                            <select name="is_normal"  lay-filter="is_sole" id="is_normal">
                                <option value="">请选择</option>
                                <option value="1" >否</option>
                                <option value="2" >是</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">表状态：</label>
                        <div class="layui-input-inline">
                            <select name="status" >
                                <option value="" >请选择</option>
                                <option value="1" >正常</option>
                                <option value="2" >停用</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button class="layui-btn icon-btn layui-btn-sm" data-id="table_id" lay-filter="search"
                                lay-submit="">
                            <i class="layui-icon" ></i>搜索
                        </button>
                        <button type="reset" class="layui-btn icon-btn layui-btn-normal layui-btn-sm"><i
                                class="layui-icon layui-icon-refresh"></i>{:__('Reset')}
                        </button>
                        <button class="{:node('WatchMaintainList/export')} layui-btn layui-bg-black iconfont icon-daochu layui-btn-sm" lay-submit id="export" >导出</button>
                    </div>
                </div>
            </form>
        </div>
    </div>


    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-hide" id="table_id" lay-filter="table_id"></table>
        </div>
    </div>

</div>
{/block}

{block name="js"} <!--js处理区-->


<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button type="button" lay-event="add"  class="layui-btn {:node('WatchMaintainList/addAdmin')} layui-btn-sm">添加</button>
        <button type="button" lay-event="reload" class="layui-btn  layui-btn-sm  layui-btn-primary">刷新表格</button>
        <button type="button" lay-event="getWatch"  class="layui-btn {:node('WatchMaintainList/getWatch')} layui-btn-sm">同步电表信息</button>
    </div>
</script>
<script type="text/html" id="operationTpl">

    <a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-xs {:node('WatchMaintainList/addAdmin')}" data-title="编辑"
       lay-event="edit"><i class="layui-icon"></i></a>
    <a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs {:node('WatchMaintainList/del')}" data-title="删除"

       lay-event="del"><i class="layui-icon "></i></a>

    {{# if(d.switching==1){ }}
<!--    <a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-xs {:node('WatchMaintainList/switching')}" data-title="拉闸"-->
<!--       lay-event="switching">拉闸</i></a>-->
    {{# }else{ }}
<!--    <a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-xs {:node('WatchMaintainList/switching')}" data-title="合闸"-->
<!--       lay-event="switching">合闸</i></a>-->

    {{# } }}
</script>
<script>

    layui.use(['element', 'table', 'form', 'jquery', 'lucky', 'laydate','opTable'], function () {
        var element = layui.element;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.jquery;
        var lucky = layui.lucky;
        var opTable = layui.opTable;
        var laydate = layui.laydate;
        form.render();
        laydate.render({
            elem: '#time1',
            range: true,
            trigger: 'click'
        });

        var myTables = table.render({
            elem: '#table_id',
            url: "{:url('index')}",
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print'],
            even: true, //开启隔行背景
            id: 'table_id',
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip', 'last'] //自定义分页布局
                , groups: 5 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页
                , limit: 20
                , limits: [10, 20, 30, 50]
            },
            // height: 'full-100',100',
            text: {
                none: '暂无相关数据'
            },
            cols: [[
                {title: '', type: 'checkbox', fixed: true},
                {title: 'ID', field: 'id',align: 'center',width:60},
                {title: '商户', field: 'name',width:100,templet:function (item){
                    if(item.name==''||item.name=='-'||item.name=='null'){
                        if(item.company==null){
                            return '暂无关联'
                        }
                        return item.company
                    }else {
                        if(item.name==null){
                            return '暂无关联'
                        }else {
                            return item.name
                        }

                    }
                    }},
                {title: '设备类型', field: 'type',width:100,templet:function (item){
                    if(item.type==1){
                        return '智能电表';
                    }else if(item.type==2){
                        return '智能水表'
                    }else if(item.type==3){
                        return '普通电表'
                    }else if(item.type==4){
                        return '普通水表'
                    }
                        return item.type == 1?'电表':'水表'
                    }},
                {title: '使用类型', field: 'use_type',width:120,templet:function (item){
                        if(item.use_type==1){
                            return '写字楼自用';
                        }else if(item.use_type==2){
                            return '写字楼公区'
                        }else if(item.use_type==3){
                            return '写字楼商户'
                        }else if(item.use_type==4){
                            return '商场自用'
                        }else if(item.use_type==5){
                            return '商场公区'
                        }else if(item.use_type==6){
                            return '商场商户'
                        }else {
                            return '暂无配置'
                    }}},
                {title: '设备编号', field: 'pointId',width:150},
                {title: '设备名称', field: 'pointName'},
                {title: '倍率', field: 'rate'},
                {title: '装表读数', field: 'initial_degrees'},
                {title: '状态', field: 'status'},
                {title: '操作', field: 'status',templet:function (item) {

                    if (item.status == "停用"){
                        return "<a class=\"layui-btn layui-btn-xs\" lay-event=\"start\">启用</a>"
                    }
                    if (item.status == "正常"){
                            return "<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"stop\">停用</a>"
                        }
                    }},

                {templet: '#operationTpl', width: 160, align: 'center', title: '操作'}

            ]],
            //  展开的列配置
            openCols: [
                {field: 'desc', title: '描述信息'}
            ],
            done: function (res) {
                layer.closeAll('loading');
            }
        });

        //导出事件
        $('#export').on('click',function () {
            var merchant_id = $('#merchant_id').val() ;
            var contract_code = $('#contract_code').val() ;
            var pointId = $('#pointId').val() ;
            var pay_status = $('#pay_status').val() ;
            var settlement_time = $('#settlement_time').val() ;
            var url = "{:url('export')}";
            window.open(url);
        });

        //导出时阻止页面刷新
        $(".layui-form").submit(function (e) {
            e.preventDefault();
        })
        /**
         * 监听单行工具操作
         */
        table.on('tool(table_id)', function (obj) {
            var data = obj.data;
            var _id = parseInt(data.id);
            var layEvent = obj.event;
            if (layEvent === "edit") {
                var urls = "{:url('add')}?id=" + _id;
                lucky.CreateOpenForm("编辑", urls);

            } else if (layEvent === "del") {
                lucky.FormatData(_id, "{:url('del')}");
            } else if (layEvent === "switching") {
                lucky.FormatData(_id, "{:url('switching')}",'确认操作?');
            }else if (layEvent === "stop"){
                layer.confirm('确定停用?', {icon: 3, title:'提示'}, function(index){
                    var urls = "{:url('stop')}?id=" + _id;
                    $.get(urls,function (res) {
                        res = JSON.parse(res)
                        if (res.code == 1){
                            layer.msg(res.msg)
                            window.location.reload()
                            return
                        }else{
                            layer.msg(res.msg)
                        }

                    })
                });

            }if (layEvent === "start"){
                layer.confirm('确定启用?', {icon: 3, title:'提示'}, function(index){
                    var urls = "{:url('stop')}?id=" + _id;
                    $.get(urls,function (res) {
                        res = JSON.parse(res)
                        if (res.code == 1){
                            layer.msg(res.msg)
                            window.location.reload()
                            return
                        }else{
                            layer.msg(res.msg)
                        }

                    })
                });

            }
        });


        table.on('toolbar(table_id)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            if (obj.event === "add") {
                lucky.CreateOpenForm("添加 ", "{:url('add')}");
                return false;
            } else if (obj.event === "close") {
                opTables.closeAll();
            } else if (obj.event === "open") {
                opTables.openAll();
            } else if (obj.event === "reload") {
                lucky.CreateReload();
            } else if (obj.event === "getWatch") {
                lucky.FormSubmit("{:url('getWatch')}");
            } else if (obj.event === "del") {

                var num = 0;
                var id = [];
                for (var i in data) {
                    num++;
                    id.push(data[i].id);
                }
                if (num < 1) {
                    layer.msg("请选择一项", {time: 1500});
                    return false;
                }
                lucky.FormatData(id, "{:url('del')}");
                return false;
            }
        });

        /**
         * 关键词搜索树
         */
        form.on('submit(search)', function (obj) {
            lucky.CreateSearch(obj.field);
            return false;
        });
    });

</script>

{/block}
